home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / rogue / random.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-05-13  |  1.5 KB  |  86 lines

  1. static long rntb[32] = {
  2.              3, 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 
  3.     0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb, 0x7449e56b,
  4.     0xbeb1dbb0, 0xab5c5918, 0x946554fd, 0x8c2e680f, 0xeb3d799f,
  5.     0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88, 0xe369735d,
  6.     0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc, 
  7.     0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e,
  8.     0x8999220b, 0x27fb47b9
  9. };
  10.  
  11. static long *fptr = &rntb[4];
  12. static long *rptr = &rntb[1];
  13. static long *state = &rntb[1];
  14. static int rand_type = 3;
  15. static int rand_deg = 31;
  16. static int rand_sep = 3;
  17. static long *end_ptr = &rntb[32];
  18.  
  19. srrandom(x)
  20. int x;
  21. {
  22.     register int i;
  23.     long rrandom();
  24.  
  25.     state[0] = (long) x;
  26.     if (rand_type != 0) {
  27.         for (i = 1; i < rand_deg; i++) {
  28.             state[i] = 1103515245 * state[i - 1] + 12345;
  29.         }
  30.         fptr = &state[rand_sep];
  31.         rptr = &state[0];
  32.         for (i = 0; i < 10*rand_deg; i++) {
  33.             (void) rrandom();
  34.         }
  35.     }
  36. }
  37.  
  38. long
  39. rrandom()
  40. {
  41.     long i;
  42.     
  43.     if (rand_type == 0) {
  44.         i = state[0] = (state[0]*1103515245 + 12345) & 0x7fffffff;
  45.     } else {
  46.         *fptr += *rptr;
  47.         i = (*fptr >> 1) & 0x7fffffff;
  48.         if (++fptr >= end_ptr) {
  49.             fptr = state;
  50.             ++rptr;
  51.         } else {
  52.             if (++rptr >= end_ptr) {
  53.                 rptr = state;
  54.             }
  55.         }
  56.     }
  57.     return(i);
  58. }
  59.  
  60. get_rand(x, y)
  61. register int x, y;
  62. {
  63.     register int r, t;
  64.  
  65.     if (x > y) {
  66.         t = y;
  67.         y = x;
  68.         x = t;
  69.     }
  70.     r = (int) rrandom();
  71.     r = (r % ((y-x)+1)) + x;
  72.     return(r);
  73. }
  74.  
  75. rand_percent(percentage)
  76. register int percentage;
  77. {
  78.     return(get_rand(1, 100) <= percentage);
  79. }
  80.  
  81. coin_toss()
  82. {
  83.  
  84.     return(((rrandom() & 01) ? 1 : 0));
  85. }
  86.